package org.goyo.main.domain.dao.sale.impl;

import java.math.BigDecimal;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.goyo.common.exception.DBException;
import org.goyo.common.utils.Pager;
import org.goyo.common.utils.ToolUtils;
import org.goyo.main.domain.dao.BaseDAO;
import org.goyo.main.domain.dao.sale.ISaleOrderDao;
import org.goyo.main.domain.entity.sale.SaleOrderEntry;
import org.hibernate.SQLQuery;
import org.springframework.stereotype.Repository;
@Repository
public class SaleOrderDaoImpl extends BaseDAO implements ISaleOrderDao{

	@Override
	public Pager findByCond(String cond, Pager pager,Object[] groupUuids) throws DBException {
		StringBuffer buf = new StringBuffer("from SaleOrder where 1=1 ");
		buf.append(" and depGroup in ");
		buf.append(ToolUtils.getInParamter(groupUuids));
		if (StringUtils.isNotBlank(cond)) {
		  buf.append(" and ");
          buf.append(cond);
		}
	    String hql = this.addSort(buf, pager);
	    String countHql = "select count(*) "+buf.toString();
		return this.findObjectsByPager(hql,countHql, pager);
	}

	@Override
	public List<SaleOrderEntry> findByParentId(String parentId) throws DBException {
		String hql = "from SaleOrderEntry where parentId=?";
		return this.findObjects(hql, null, new Object[]{parentId});
	}

	@Override
	public void updateSumReceiptAmount(BigDecimal amount, String uuid) throws DBException {
		String hql = "update SaleOrder set sumReceiptAmount=?  where uuid=?";
		this.updateByHql(hql, new Object[]{amount,uuid});
	}

	@Override
	public Pager findByEntryCond(String cond, Pager pager) throws DBException {
		StringBuffer buf = new StringBuffer("from SaleOrderEntry where 1=1 ");
		if (StringUtils.isNotBlank(cond)) {
		  buf.append(" and ");
          buf.append(cond);
		}
	    String hql = this.addSort(buf, pager);
	    String countHql = "select count(*) "+buf.toString();
		return this.findObjectsByPager(hql,countHql, pager);
	}
	@Override
	public void updateSumOutQtyByUuid(String uuid)
			throws DBException {
		 StringBuffer sql = new StringBuffer("UPDATE t_sale_order_entry  SET sum_out_qty = ");
		 sql.append("(");
		 sql.append("(SELECT IFNULL(SUM(t.out_storage_qty),0) FROM t_dispatch_entry t1 LEFT JOIN t_sale_out_storage_entry t  ON  t1.uuid = t.dispatchEntryId WHERE t1.saleOrderEntryId =?)");
		 sql.append(" + ");
		 sql.append("(SELECT IFNULL(SUM(out_storage_qty),0) FROM t_sale_out_storage_entry WHERE saleOrderEntryId =?))");
		 sql.append("WHERE uuid=?");
		 SQLQuery query = this.getSession().createSQLQuery(sql.toString());
		 query.setParameter(0, uuid);
		 query.setParameter(1, uuid);
		 query.setParameter(2, uuid);
		 query.executeUpdate();
//		this.updateByHql(sql.toString(), new Object[]{uuid,uuid,uuid});
	}

	@Override
	public void updateSumDispQtyByUuid(String uuid) throws DBException {
		String hql = "UPDATE t_sale_order_entry SET sum_disp_qty = (SELECT SUM(dispatch_qty) FROM t_dispatch_entry WHERE saleOrderEntryId =?) WHERE  UUID=?";
		SQLQuery query = this.getSession().createSQLQuery(hql);
		query.setParameter(0, uuid);
		query.setParameter(1, uuid);
		query.executeUpdate();
		
//		this.(hql, new Object[]{uuid,uuid});
	}

	@Override
	public void updateSumQuitQtyByUuid(String uuid) throws DBException {
		String hql = "UPDATE t_sale_order_entry SET sum_disp_qty = (SELECT SUM(dispatch_qty) FROM t_dispatch_entry WHERE saleOrderEntryId =?) WHERE  UUID=?";
		SQLQuery query = this.getSession().createSQLQuery(hql);
		query.setParameter(0, uuid);
		query.setParameter(1, uuid);
		query.executeUpdate();

//		this.updateByHql(hql, new Object[]{uuid,uuid});
	}
}
